package com.seannews.client.api;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.seannews.server.Pojo.News;
import com.seannews.server.Pojo.User;
import com.seannews.server.Service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.FileOutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Resource
    private UserService userService;

    @CrossOrigin
    @GetMapping("/login")
    @ResponseBody
    public HashMap<String,Object> login(String userId,String userPasswd)
    {
        HashMap<String,Object> result = new HashMap<>();
        User userinfo=userService.login(userId,userPasswd);
        if(userinfo!=null)
        {
            result.put("loginresult","登录成功");
            result.put("User",userinfo);
        }else{
            result.put("loginresult","登录失败");
        }
        System.out.println(result);
        return  result;
    }

    @CrossOrigin
    @GetMapping("/sign")
    @ResponseBody
    public HashMap<String,Object> sign(String userId,String userPasswd,String userName,int userType)
    {
        HashMap<String,Object> result = new HashMap<>();
        User userinfo=userService.login(userId,userPasswd);
        if(userinfo==null)
        {
            User user=userService.sign(userId,userPasswd,userName,userType);
            result.put("loginresult","注册成功");
            result.put("userinfo",user);
        }else{
            result.put("loginresult","注册失败，用户已存在");
        }
        System.out.println(result);
        return  result;
    }
    @CrossOrigin
    @GetMapping("/getword")
    @ResponseBody
    public String getword(String base64)
    {
        return  userService.pictureword(base64);
    }
    @CrossOrigin
    @GetMapping("/getrandomid")
    @ResponseBody
    public String getrandomid()
    {
        return  userService.getrandomid();
    }
    @CrossOrigin
    @GetMapping("/getalluser")
    @ResponseBody
    public String getalluser(int page,int limit) throws JsonProcessingException {
        ObjectMapper om=new ObjectMapper();
        List<User> data=userService.getalluser((page-1)*limit,limit);
        String json=om.writeValueAsString(data);
        String a="{\"code\":0,\"count\":"+userService.getUsercount()+",\"data\":"+json+",\"msg\":\"\"}";
        return a;
    }
    @CrossOrigin
    @GetMapping("/addreadtype")
    @ResponseBody
    public void addreadtype(String userid,String type) {
        userService.addreadtype(userid,type);
    }
    @CrossOrigin
    @GetMapping("/getuserhabbit")
    @ResponseBody
    public String getuserhabbit(String userid) {
        User user=userService.getuserbyid(userid);
        return user.gethabbit();
    }
}
